import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hj_crm/entity/app_config.dart';
import 'package:hj_crm/entity/user_info.dart';
import 'package:hj_crm/utils/sp_key.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SpUtil {
  static get sp => Get.find<SharedPreferences>();

  /// 存储APP配置信息
  /// [config] Config
  static updateConfig(AppConfig config) {
    sp.setString(SpKey.keyConfig, jsonEncode(config.toJson()));
  }

  /// AppConfig 配置信息
  /// [config] Config
  static AppConfig? getConfig() {
    try {
      var json = sp.getString(SpKey.keyConfig);
      if (json == null) {
        return null;
      } else {
        return AppConfig.fromJson(jsonDecode(json));
      }
    } catch (e) {
      debugPrint(e.toString());
      return null;
    }
  }

  /// 更新是否安装
  /// [installed] 是否安装
  static updateInstalled(bool installed) {
    sp.setBool(SpKey.keyInstalled, installed);
  }

  /// 是否安装
  static bool isInstalled() {
    try {
      bool? isInstalled = sp.getBool(SpKey.keyInstalled);

      return isInstalled ?? false;
    } catch (e) {
      debugPrint(e.toString());
      return false;
    }
  }

  /// 更新Token
  /// [token] Token
  static updateToken(String token) {
    sp.setString(SpKey.keyToken, token);
  }

  /// 获取Token
  static String? getToken() {
    try {
      var token = sp.getString(SpKey.keyToken);
      return token;
    } catch (e) {
      debugPrint(e.toString());
      return null;
    }
  }

  ///删除存储Token
  static deleteToken() => sp.remove(SpKey.keyToken);

  /// 双系统
  static bool dualSystem() {
    try {
      bool? dualSystem = sp.getBool(SpKey.keyDaulSystem);

      return dualSystem ?? false;
    } catch (e) {
      debugPrint(e.toString());
      return false;
    }
  }

  static updateDualSystem(bool dualSystem) {
    sp.setBool(SpKey.keyDaulSystem, dualSystem);
  }

  ///删除存储用户信息
  static deleteUserInfo() => sp.remove(SpKey.keyUserInfo);

  ///存储用户信息
  ///[userInfo] 用户信息
  static putUserInfo(UserInfo userInfo) =>
      sp.setString(SpKey.keyUserInfo, jsonEncode(userInfo.toJson()));

  ///获取用户信息
  ///[findUserInfo] 用户信息
  static UserInfo? findUserInfo() {
    try {
      var json = sp.getString(SpKey.keyUserInfo);
      if (json == null) {
        return null;
      } else {
        return UserInfo.fromJson(jsonDecode(json));
      }
    } catch (e) {
      debugPrint(e.toString());
      return null;
    }
  }

  static deletePreference(String key) => sp.remove(key);

  static savePreference(String key, Object value) {
    if (value is int) {
      Get.find<SharedPreferences>().setInt(key, value);
    } else if (value is double) {
      Get.find<SharedPreferences>().setDouble(key, value);
    } else if (value is bool) {
      Get.find<SharedPreferences>().setBool(key, value);
    } else if (value is String) {
      Get.find<SharedPreferences>().setString(key, value);
    } else if (value is List) {
      // Get.find<SharedPreferences>().setStringList(key, value);
    } else {
      throw Exception("不能得到这种类型");
    }
  }

  ///
  /// 取数据
  ///
  static dynamic getPreference(String key, Object defaultValue) {
    if (defaultValue is int) {
      return sp.getInt(key);
    } else if (defaultValue is double) {
      return sp.getDouble(key);
    } else if (defaultValue is bool) {
      return sp.getBool(key);
    } else if (defaultValue is String) {
      if (null == sp.getString(key)) {
        return defaultValue;
      } else {
        return sp.getString(key);
      }
    } else if (defaultValue is List) {
      return sp.getStringList(key);
    } else {
      throw Exception("不能得到这种类型");
    }
  }
}
